import Cache from '@/utils/cache.js'
import { ACTIVITY_OPEN, ACTIVITY_GUIDE, ACTIVITY_ISBLACK, ACTIVITY_NEW, ACTIVITY_NO_QUERY, ACTIVITY_HAD_SHOW, ACTIVITY_M_HAD_SHOW } from '@/config/cache'
import { getUserStateApi } from '@/api/activity.js'

export default {
    state: {
        open: Cache.get(ACTIVITY_OPEN) || false,
        isNew: Cache.get(ACTIVITY_NEW) || false,
        noQuery: Cache.get(ACTIVITY_NO_QUERY) || false,
        hadShow: Cache.get(ACTIVITY_HAD_SHOW) || false,
        hadShowMoney: Cache.get(ACTIVITY_M_HAD_SHOW) || false,
        isBlack: Cache.get(ACTIVITY_ISBLACK) || false,
        showGuide: Cache.get(ACTIVITY_GUIDE) || false
    },
    actions: {
        // 获取活动状态
        async GET_ACTIVITY({ commit }) {
            const res = await getUserStateApi()
            const { newUser = false, checked = false, activity = false, isBlack: balckMap = {} } = res.data || {}
            console.log('活动状态：newUser：', newUser, ' checked：', checked, '  activity；',activity, '  isBlack', balckMap )
            const blackList = Object.keys(balckMap)
            const isBlack = blackList.find(id => balckMap[id]) ?? false
            commit('SET_ACTIVITY', {
                isNew: newUser,
                open: activity,
                noQuery: !checked,
                isBlack
            })
        },
           // 停止活动展示
        CLOSE_ACTIVITY({commit}) {
            commit('SET_ACTIVITY', {
                open: false,
            })
        }
    },
    mutations: {
        // 标记引导
        SET_ACTIVITY_GUIDE_SHOW(state, payload) {
            state.showGuide = !!payload
            Cache.set(ACTIVITY_GUIDE, !!payload)
        },
        // 标记活动弹出状态
        SET_ACTIVITY_SHOW(state, payload = false) {
            state.hadShow = !!payload
            Cache.set(ACTIVITY_HAD_SHOW, !!payload)
        },
        // 标记活动金额弹出
        SET_ACTIVITY_M_SHOW(state, payload) {
            state.hadShowMoney = !!payload
            Cache.set(ACTIVITY_M_HAD_SHOW, !!payload)
        },
        // 设置活动状态
        SET_ACTIVITY(state, payload) {
            const { open, isNew, noQuery, isBlack } = payload
            state.open = open
            state.isNew = isNew
            state.noQuery = noQuery
            state.isBlack = isBlack
            Cache.set(ACTIVITY_OPEN, open)
            Cache.set(ACTIVITY_NEW, isNew)
            Cache.set(ACTIVITY_NO_QUERY, noQuery)
            Cache.set(ACTIVITY_ISBLACK, isBlack)
            console.log(JSON.stringify(state), ' -----')
        },
      
    },
}